package com.fractal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class PutDatabase extends IntentService {
	public PutDatabase() {
		super("PutDatabase");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Log.d("debug", "Starting Service..");
		
		Bundle b = intent.getExtras();
		
		String x0 = b.getString("x0");
		String y0 = b.getString("y0");
		String psize = b.getString("psize");
		postData(x0, y0, psize);
	}
	
	public void postData(String x0, String y0, String psize) {
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost("http://www.kilza.dk/smap/index.php");
	    Log.d("debug", "Got these info: " + x0 + " , " + y0 + " , " + psize);

	    try {
	        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
	        nameValuePairs.add(new BasicNameValuePair("command", "save"));
	        nameValuePairs.add(new BasicNameValuePair("x0", x0));
	        nameValuePairs.add(new BasicNameValuePair("y0", y0));
	        nameValuePairs.add(new BasicNameValuePair("psize", psize));
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	        HttpResponse response = httpclient.execute(httppost);
	        
	    } catch (ClientProtocolException e) {
	    	Log.d("debug", "ClientProtocolException...");
	    } catch (IOException e) {
	    	Log.d("debug", "IOException...");
	    }
	} 

}
